package com.lottery.im.module.service.ui;

import android.support.v7.widget.AppCompatButton;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.lottery.im.R;
import com.lottery.im.api.result.entity.CanReceiveData0;
import com.lottery.im.api.result.entity.CanReceiveData1;
import com.lottery.im.api.result.entity.SignData;
import com.lottery.im.api.result.entity.SignDates;
import com.lottery.im.api.result.entity.SignDayOfMonth;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.module.service.adapter.SignAdapter;
import com.lottery.im.module.service.presenter.SigninPresenter;
import com.lottery.im.module.service.view.SigninView;
import com.lottery.im.util.ToastUtils;
import com.lottery.im.widget.SignView;
import com.lottery.im.widget.dialog.SignDialogFragment;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class SigninFragment extends BaseSwipeFragment<SigninPresenter> implements SigninView {

  @BindView(R.id.tv_title) TextView tvTitle;
  @BindView(R.id.tv_hunders) TextView tvHunders;
  @BindView(R.id.tv_tens) TextView tvTens;
  @BindView(R.id.tv_ones) TextView tvOnes;
  @BindView(R.id.tv_day_continues) TextView tvDayContinues;
  @BindView(R.id.tv_canReceive0) TextView tvCanReceive0;
  @BindView(R.id.btn_canRecevie0) Button btnCanRecevie0;
  @BindView(R.id.tv_canReceive1) TextView tvCanReceive1;
  @BindView(R.id.btn_canRecevie1) Button btnCanRecevie1;
  @BindView(R.id.tv_month) TextView tvMonth;
  @BindView(R.id.signView) SignView signView;
  @BindView(R.id.ab_sign) AppCompatButton abSign;
  @BindView(R.id.tv_signDetail) TextView tvSignDetail;
  private List<SignDates> data = new ArrayList<>();
  private List<Integer> dates = new ArrayList<>();
  private int mMonth;
  private int mYear;
  private String mYearMonth;

  private int addMonths=0;
  private SignAdapter mSignAdapter;

  @Override protected int getLayoutResId() {
    return R.layout.fragment_signin;
  }

  @Override protected void initPresenter() {
    presenter = new SigninPresenter(getActivity(), this, this);
    presenter.init();
  }

  @Override public void initView() {
    initMonth();
    tvTitle.setText("签到");
    mYearMonth = mMonth < 10 ? "0" + mMonth : mMonth + "";
    presenter.loadUserSignData(mYear + mYearMonth);
    presenter.loadCanReceiveData0("0");
    presenter.loadCanReceiveData1("1");
  }

  private void initMonth() {
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.MONTH, addMonths);
    mYear = calendar.get(Calendar.YEAR);
    mMonth = calendar.get(Calendar.MONTH)+1;
    tvMonth.setText(String.valueOf(mYear) + "年"
        + getResources().getStringArray(R.array.month_array)[mMonth-1]);
  }

  private void setSignDateView(SignData returnMapBean) {
    data.clear();dates.clear();
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.MONTH, addMonths);
    int dayOfMonthToday = calendar.get(Calendar.DAY_OF_MONTH);
    List<SignDayOfMonth> list = returnMapBean.getUserSignList();
    for (int i = 0; i < list.size(); i++) {
      String dayOfMonth = list.get(i).getSign_date().substring(6, 8);
      dayOfMonth = Integer.valueOf(dayOfMonth) >= 10 ? dayOfMonth : dayOfMonth.substring(1, 2);
      dates.add(Integer.valueOf(dayOfMonth));
    }

    if(dates.contains(dayOfMonthToday)){
      abSign.setEnabled(false);
      abSign.setText("己签到");
      abSign.setCompoundDrawablesWithIntrinsicBounds(
          getResources().getDrawable(R.drawable.icon_yqd),null,null,null);
      abSign.setCompoundDrawablePadding(10);
    }

    for (int j = 0; j < 31; j++) {
      SignDates signDates = new SignDates();
      if (dates.contains(j + 1)) {
        signDates.setDayType(0);//有签到过的
      } else {
        signDates.setDayType(1);
      }

      //当月今天标记红色
      if (dayOfMonthToday == j + 1 && !dates.contains(j + 1)&&addMonths==0) {
        signDates.setDayType(2);
      }

      data.add(signDates);
    }

    signView.setAddMonths(addMonths);
    signView.initData();
    if(mSignAdapter==null){
      mSignAdapter = new SignAdapter(data);
      signView.setAdapter(mSignAdapter);
    }else{
      signView.notifyDataSetChanged();
    }
  }

  //签到天数
  @Override public void loadUserSignDataSuccess(SignData returnMapBean) {
    int num = returnMapBean.getSignCount();
    tvDayContinues.setText("己连续签到"+String.valueOf(returnMapBean.getSignContinuteNum())+"天");
    tvHunders.setText(String.valueOf(num / 100));
    tvTens.setText(String.valueOf(num / 10 % 10));
    tvOnes.setText(String.valueOf(num % 10));

    setSignDateView(returnMapBean);
  }

  @Override public void loadCanReceiveData0Success(CanReceiveData0.ReturnMapBean canReceiveData0
      , String returnCode) {
    if(canReceiveData0!=null){
      tvCanReceive0.setText(canReceiveData0.getValue());
      btnCanRecevie0.setText(canReceiveData0.getBtnMsg(returnCode));
      btnCanRecevie0.setEnabled(canReceiveData0.isCanClick(returnCode));

      tvSignDetail.setText(canReceiveData0.getSignDetail());
    }
  }

  @Override public void loadCanReciveData1Success(CanReceiveData1.ReturnMapBean canReceiveData1
      , String returnCode) {
    if(canReceiveData1!=null){
      tvCanReceive1.setText(canReceiveData1.getValue());
      btnCanRecevie1.setText(canReceiveData1.getBtnMsg(returnCode));
      btnCanRecevie1.setEnabled(canReceiveData1.isCanClick(returnCode));
    }
  }

  @Override public void userSignDataSuccess() {
    SignDialogFragment fragment = SignDialogFragment.newInstance();
    fragment.show(getActivity().getSupportFragmentManager(), null);
    presenter.loadUserSignData(mYear + mYearMonth);
    presenter.loadCanReceiveData0("0");
    presenter.loadCanReceiveData1("1");
  }

  @OnClick({ R.id.iv_back, R.id.btn_canRecevie0, R.id.btn_canRecevie1,
      R.id.ab_sign,R.id.img_left,R.id.img_right })
  public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.iv_back:
        popBackStack();
        break;
      case R.id.btn_canRecevie0:
        presenter.onReceiveSignClick("0");
        break;
      case R.id.btn_canRecevie1:
        presenter.onReceiveSignClick1("1");
        break;
      case R.id.ab_sign:
        presenter.onSignClick();
        break;
      case R.id.img_left:
        changeMonth(addMonths - 1);
        break;
      case R.id.img_right:
        if(addMonths<0){
          changeMonth(addMonths+1);
        }else{
          ToastUtils.showToast(mActivity,"不能查看大于当前月份");
        }
        break;
    }
  }

  private void changeMonth(int i) {
    addMonths = i;
    initMonth();
    mYearMonth = mMonth < 10 ? "0" + mMonth : mMonth + "";
    presenter.loadUserSignData(mYear + mYearMonth);
  }
}
